'''
Defines django interaction with the HTML temlplates. requires further dosumentation.
'''
# Import django modules
from auth.forms import RegisterForm
from auth.models import UserProfile
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
from django.core.context_processors import csrf
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template.loader import render_to_string
from maps.forms import EventForm2, CommentForm
from maps.models import Event, Comment, Tag
from mapaimprez.menu import MainLoggedIn, MainLoggedOut

# Import custom modules


def index(request):
    'Display a map'
    events = None
    
    #events = Event.objects.order_by('name') #request.user.get_profile().events.all()  - gets the users events.
    if request.user.is_authenticated():
        events = request.user.get_profile().events.all()
    
    if request.user.is_authenticated():
        menu = MainLoggedIn
    else:
        menu = MainLoggedOut
        
    return render_to_response('maps/index.html', {
        'events': events,
        'content': render_to_string('maps/events.html', {'events': events}),
        'menu' : menu,
        'template_menu' : 'menu.html'})
    
    
def add_event(request): 
    'Add new event'
    
    if request.method == 'POST':
        form = EventForm2(request.POST)
        #form.save()
        if form.is_valid():  
            #form.save()
            event = form.save(False)    #creates a database object to be finished and saved
            tags = request.POST.get('tags').split(",")
            taglist = []
            for tag in tags:
                tmp = Tag.objects.filter(name=tag)
                if tmp.__len__()!=0:
                    taglist.append(tmp[0])
                else:
                    tmp2 = Tag(name=tag)
                    tmp2.save()
                    taglist.append(tmp2) 
            #print taglist
            #event.tags = taglist
            #tag valdation, fix adding the location?        
            event.save()
            event.tags = taglist
            #event.save()
            return HttpResponse("Event successfully added" )
        else:
            return HttpResponse("Form invalid" )
    else:
        
        if request.user.is_authenticated():
            menu = MainLoggedIn
        else:
            menu = MainLoggedOut
        
        form = EventForm2()
        c = {'form': form,
         'menu' : menu,
         'template_menu' : 'menu.html'}
        
        c.update(csrf(request))
        return render_to_response("maps/add_event.html",  c) 

def details(request, event_id):
    'Display event details. Save comments.'
    if request.method == 'POST':
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit = False)
            comment.user = UserProfile.objects.get(user=request.user)
            comment.event = Event.objects.get(id=event_id)
            comment.save()
        else:
            return HttpResponse("Could not add the comment")
    
    if request.user.is_authenticated():
        menu = MainLoggedIn
    else:
        menu = MainLoggedOut
    
    form = CommentForm()
    argevent = Event.objects.get(id=event_id)
    
    c = {'form': form,
         'event':argevent ,
         'comments': render_to_string('maps/comments.html', {'comments': Comment.objects.filter(event=argevent)}),
         'menu' : menu,
         'template_menu' : 'menu.html'}
    c.update(csrf(request))
    #return render_to_response('maps/details.html', {'event':Event.objects.get(id=event_id)})
    return render_to_response('maps/details.html', c)

